<template>
  <!-- 企业table组件 -->
  <div>
    <transition name="el-zoom-in-top">
      <div class="download" v-show="checkboxSelect > 0">
        <i class="el-icon-info"></i>
        共{{ total }}项，已选择<b>{{ checkboxSelect }}</b
        >项
        <span @click="excelDownload">下载</span>
        <em @click="cleanSelection">清空</em>
      </div>
    </transition>
    <el-table
      :data="tableData"
      @selectionChange="downloadWhether"
      ref="multipleTable"
      stripe
      style="width: 100%"
      :row-key="getRowKeys"
      @selection-change="handleSelectionChange"
      @sort-change="sortChange"
    >
      <template v-if="platformId != undefined">
        <el-table-column width="45"> </el-table-column>
        <el-table-column prop="name" sortable="custom" label="企业名称"> </el-table-column>
      </template>
      <template v-else>
        <el-table-column type="selection" :reserve-selection="true" width="45"> </el-table-column>
        <el-table-column prop="businessName" sortable="custom" label="企业名称">
        </el-table-column>
      </template>
      <el-table-column prop="industyMsg" label="类型"> </el-table-column>
      <el-table-column prop="socialCreditCode" sortable="custom" label="统一社会信用代码">
      </el-table-column>
      <el-table-column
        v-if="platformId == undefined"
        prop="platformName"
        label="用工平台"
      >
      </el-table-column>
      <el-table-column prop="taskNum" sortable="custom" align="center" label="任务数量">
        <template #default="item">
          {{ item.row.taskNum == null ? 0 : item.row.taskNum }}
        </template>
      </el-table-column>
      <template v-if="platformId != undefined">
        <el-table-column prop="allFee" sortable="custom" label="累计结算额">
          <template #default="item">
            {{ formatNumber(item.row.allFee) }}
          </template>
        </el-table-column>
      </template>
      <template v-else>
        <el-table-column prop="taskPaymentAll" sortable="custom" label="累计结算额">
          <template #default="item">
            {{ formatNumber(item.row.taskPaymentAll) }}
          </template>
        </el-table-column>
      </template>
      <el-table-column
        prop="caozuo"
        label="操作"
        width="70"
        class-name="editor"
      >
        <template #default="item">
          <router-link
            v-if="platformId"
            :to="{ path: '/businessDel', query: { id: item.row.id } }"
          >
            <el-button type="text">详情</el-button>
          </router-link>
          <router-link
            v-else
            :to="{ path: '/businessDel', query: { id: item.row.businessId } }"
          >
            <el-button type="text">详情</el-button>
          </router-link>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      v-if="tableData.length > 0"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-sizes="arrPage"
      :page-size="selectPage"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>
  </div>
</template>

<script>
import axios from "axios";
export default {
  props: ["search", "platformId"],
  data() {
    return {
      getRowKeys(row) {
        // 获取row的key值
        return row.id;
      },
      orderByZH: false,
      orderByAsc: false,
      orderByColume: 'createTime',
      tableData: [],
      currentPage: 1,
      total: 0, // 总条数
      arrPage: [10, 20], // 每页条数
      selectPage: 10, // 每页选中条数
      checkboxSelect: 0, // 选中条数
			dwonloadList: [] // 选中的数据
    };
  },
  mounted() {
    if (this.platformId) {
      this.getplatformData();
    } else {
      this.getTableData();
    }
  },
  methods: {
    getTableData() {
      // 获取列表信息
      this.$http({
        url: "/api/business/selectBusinessInfoByMixed",
        method: "post",
        headers: {
          "Content-Type": "application/json",
        },
        data: {
          mixedStr: this.search,
          pageNum: this.currentPage,
          pageSize: this.selectPage,
          orderByAsc: this.orderByAsc, // 是否正序
          orderByColume: this.orderByColume, // 排序字段
          orderByZH: this.orderByZH, // 是否汉字
        },
      }).then((data) => {
        if (data.code === 0) {
          this.tableData = data.result.list;
          this.total = data.result.total;
        }
      });
    },
    getplatformData() {
      // 灵活用功详情获取企业列表
      this.$http({
        url: "/api/platform/selectBusinessInfoByPlatform",
        method: "post",
        headers: {
          "Content-Type": "application/json",
        },
        data: {
          platformId: this.platformId,
          pageNum: this.currentPage,
          pageSize: this.selectPage,
        },
      }).then((data) => {
        if (data.code === 0) {
          this.tableData = data.result.list;
          this.total = data.result.total;
        }
      });
    },
    handleSizeChange(val) {
      // 切换每页条数调用
      this.selectPage = val;
      if (this.platformId) {
        this.getplatformData();
      } else {
        this.getTableData();
      }
    },
    handleCurrentChange(val) {
      // 切换当前页数调用
      this.currentPage = val;
      if (this.platformId) {
        this.getplatformData();
      } else {
        this.getTableData();
      }
    },
    handleSelectionChange() {},
    cleanSelection() {
      // 清空选择
      this.$refs.multipleTable.clearSelection();
    },
    excelDownload() {
      // 下载
      let checkSelect = new FormData();
      for (let i = 0; i < this.dwonloadList.length; i++) {
        checkSelect.append("list", this.dwonloadList[i].businessId);
      }
      axios({
        method: "POST",
        url: "/api/business/businessExcel",
        data: checkSelect,
        responseType: "blob",
        headers: {
          Authorization: localStorage.getItem("hegui_token")
        }
      })
        .then((res) => {
          const blob = new Blob([res.data]);
          const downloadElement = document.createElement("a");
          const href = window.URL.createObjectURL(blob);
          //后台再header中传文件名
          downloadElement.href = href;
          downloadElement.download = "企业信息"+ this.format("yyyyMMddhhmmss", new Date()) +".xlsx";
          document.body.appendChild(downloadElement);
          downloadElement.click();
          document.body.removeChild(downloadElement); // 下载完成移除元素
          window.URL.revokeObjectURL(href); // 释放掉blob对象
        })
        .catch((err) => {
          console.log(err);
        });
    },
    downloadWhether(d) {
      this.checkboxSelect = d.length;
			this.dwonloadList = d
    },
    sortChange(d) {
				// 监听排序
      if(d.prop == null) {
        // 默认排序方式
        this.orderByZH = false
        this.orderByAsc = false
        this.orderByColume = 'createTime'
      }else {
        let orderByZH = false
        if(d.prop == "businessName" || d.prop == "industyMsg" || d.prop == "platformName" || d.prop == "name") {
          orderByZH = true
        }
        this.currentPage = 1
        this.selectPage = 10
        let orderByAsc = d.order == "descending" ? false : true
        this.orderByZH = orderByZH
        this.orderByAsc = orderByAsc
        this.orderByColume = d.prop
      }
      if (this.platformId) {
        this.getplatformData();
      } else {
        this.getTableData();
      }
    },
  },
};
</script>

<style>
</style>
